package cn.zysheep.config;

import cn.zysheep.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @version v1.0
 * @Author: lyw
 * @Date: 2021/7/24
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


//    @Bean
//    public UserDetailsService userDetailsService() {
//        InMemoryUserDetailsManager detailsManager = new InMemoryUserDetailsManager();
//        detailsManager.createUser(User.withUsername("zhangsan").password("123").authorities("save","update").build());
//        detailsManager.createUser(User.withUsername("lisi").password("456").authorities("save").build());
//
//        return detailsManager;
//    }



//    @Bean
//    public PasswordEncoder passwordEncoder() {
//        return NoOpPasswordEncoder.getInstance();
//    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    //配置授权服务，即安全拦截机制
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
//                .authorizeRequests()
//                .antMatchers("/user/save").hasAuthority("save")
//                .antMatchers("/user/update").hasAuthority("update")
//                .antMatchers("/user/getUserList").access("hasAuthority('save') and hasAuthority('update')")
//                .antMatchers("/user/**").authenticated()
//                .anyRequest().permitAll()
//                .and()
                .formLogin()  // 支持form表单验证
                .successForwardUrl("/user/login-success"); // 自定义登录成功请求
    }
}
